package com.ceba.test.modules.log.dao;

import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.web.dao.IBaseDao;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.enums.LogOpTypeEnum;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.modules.log.entity.OpLog;

import abc.IContextDictionary;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import abc.security.Encoder;
import abc.web.HttpContext;

import java.util.Map;

public class OpLogDao extends IBaseDao {
	static Logger logger = LoggerFactory.getLogger(OpLogDao.class);
	
	/**
	 * 获取日志列表
	 * 
	 */

	public Sql getLogsList(String txtStartTime, String txtFinishTime, String txtLogsType,String txtKey,String orderBy) throws Exception {
		Sql sql = null;
		if (!StringUtil.isEmptyString(txtStartTime) && !StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime BETWEEN '" + txtStartTime + "' AND DATE_ADD('"
					+ txtFinishTime + "',INTERVAL 1 DAY) and LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		} else if (!StringUtil.isEmptyString(txtStartTime) && StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime>='" + txtStartTime + "' and LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		} else if (StringUtil.isEmptyString(txtStartTime) && !StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime<='" + txtFinishTime + "'  and LOG_TYPE='"+txtLogsType+"'  ORDER BY"+orderBy);
		} else {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog  where MESSAGE like '%"+txtKey+"%' and  LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		}

		return sql;
	}
	
	public JSONObject getLogsList(Map<String,Object> dic){
		SqlDataSource dataSource = new SqlDataSource();
		String txtStartTime = (String)dic.get("txtStartTime");
		String txtFinishTime = (String)dic.get("txtFinishTime");
		String txtLogsType = (String)dic.get("txtLogsType");
		String txtKey = (String)dic.get("txtKey");
		String orderBy = "CREATETIME desc";
		
		Sql sql = null;
		if (!StringUtil.isEmptyString(txtStartTime) && !StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime BETWEEN '" + txtStartTime + "' AND DATE_ADD('"
					+ txtFinishTime + "',INTERVAL 1 DAY) and LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		} else if (!StringUtil.isEmptyString(txtStartTime) && StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime>='" + txtStartTime + "' and LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		} else if (StringUtil.isEmptyString(txtStartTime) && !StringUtil.isEmptyString(txtFinishTime)) {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog where MESSAGE like '%"+txtKey+"%' and createtime<='" + txtFinishTime + "'  and LOG_TYPE='"+txtLogsType+"'  ORDER BY"+orderBy);
		} else {
			sql = new Sql("select ID,MESSAGE,LOG_TYPE,CREATETIME from cb_oplog  where MESSAGE like '%"+txtKey+"%' and  LOG_TYPE='"+txtLogsType+"' ORDER BY "+orderBy);
		}
		dataSource.getSelectCommands().add(sql);
		JSONObject dt = dataSource.query(0, dic).toJSONObject();
		return dt;
	}
	
   /**
    * 导出时调用
    * @param txtStartTime
    * @param txtFinishTime
    * @param txtLogsType
    * @param txtKey
    * @param orderBy
    * @return
    */
     public DataTable  getPublicStatic(String txtStartTime,String txtFinishTime,String txtLogsType, String txtKey,String orderBy){
    	 
		try {
			 Sql sql = getLogsList( txtStartTime, txtFinishTime, txtLogsType,   txtKey, orderBy);
	    	 
	    	 DataTable dt = SqlSessionManager.getDataTable(sql);
	    	 
	    	 return dt;

		} catch (Exception e) {
			LoggerHelper.error(logger, e, "");
		}
		
		return null;
    }
	/**
	 * 添加一条记录
	 * 
	 * @return
	 */
	public Boolean add(OpLog opLog) throws Exception {
		IStore store = StoreManager.create("cb_oplog");
		IObject object = new XObject("cb_oplog");
		object.put("ID", opLog.getId());
		object.put("LOG_TYPE", opLog.getLogType());
		object.put("OP_TYPE", opLog.getOpType());
		object.put("MESSAGE", opLog.getMessage());
		object.put("ERROR_MESSAGE", opLog.getErrorMesage());
		object.put("HASH_MESSAGE", opLog.getHashMessage());
		object.put("CREATETIME", opLog.getCreateTime());
		object.setCreateTime();
		object.setCreateUser();

		if (store.insert(object) > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除一条数据
	 * 
	 * @param id
	 *            数据id
	 * @return
	 */
	public Boolean delete(String id) throws Exception {
		Sql sql = new Sql("delete from cb_oplog where id ='" + id + "'");
		return SqlSessionManager.execute(sql);

	}

	/**
	 * @return
	 */
	public DataTable getOpLog(String id) throws Exception {

		Sql sql = new Sql("SELECT * FROM cb_oplog WHERE ID=? ");
		sql.addParameter(id);
		return SqlSessionManager.getDataTable(sql);
	}
	
	/**
	 * 获取所有不用签名文件个数
	 * 
	 * @return
	 */
	public  long getAllNoSignFileCount() throws Exception {
		return SqlSessionManager.getCount(" cb_oplog", " OP_TYPE = " + LogOpTypeEnum.SIGN_NO_NEED.getType());
	}

}
